"use strict";

const _ = require("lodash");
const { metadbService } = require("../service/database/metadb");

/**
 * 登录窗口功能控制器
 * @class
 */
class settingController {

  async saveSetting(args) {
    let result = { code: 200, data: null };
    // 过滤 args，只保留 filters 中允许的键
    const filters = [
      "team",
      "subject",
      "check_weekly",
      "pages",
      "startup",
    ];
    const validArgs = Object.entries(args).filter(([key]) =>
      filters.includes(key)
    );
    // 如果没有合法的键，直接返回错误
    if (validArgs.length === 0) {
      result.msg = "No valid keys provided";
      result.code = 400;
      return result;
    }
    let promises = validArgs.map(async ([key, value]) => {
      const res = await metadbService.saveUserSetting(key, value);
      return { [key]: res };
    });

    result.data = await Promise.all(promises);
    return result;
  }

  async getUserData() {
    let result = { code: 200, data: null };

    let filters = [
      "team",
      "subject",
      "check_weekly",
      "pages",
      "startup",
    ];
    let promises = filters.map(async (arg) => {
      let res = await metadbService.getUserSetting(arg);      
      return { [arg]: res };
    });
    result.data = await Promise.all(promises);
    return result;
  }
  //初始化应用程序设置
  async getSettingMeta() {
    let args = ["team", "subject", "check_weekly", "pages"];
    const result = {
      data: null,
      code: 200,
    };
    let promises = args.map(async (arg) => {
      let res = await metadbService.getSettingMeta(arg);
      return { [arg]: res };
    });
    result.data = await Promise.all(promises);
    return result;
  }
  
}

settingController.toString = () => "[class settingController]";
module.exports = settingController;
